#include <iostream>

#include <log4cxx/logger.h>
#include <json/json.h>

#include "CnfSvrClientProtoHelper.h"
#include "CnfSvrClient.h"
#include "CnfSvrCommon.h"

using namespace log4cxx;

using std::cout;
using std::endl;
        
LoggerPtr g_logger = Logger::getLogger("CnfSvrClientProtoHelper");

string cs_build_request_get_all_cnf() {
	json_object * j = json_object_new_object();
	json_object * j_cmd_id = json_object_new_int(CMD_GET_ALL_CNF);	
	json_object_object_add(j, "CMD_ID", j_cmd_id);
	string request = json_object_to_json_string(j);
	json_object_put(j);
	return request;
}

int cs_parse_response_get_all_cnf(const char * response, int32_t & result, NS_CNF & ns, list<MDS_CNF> & l_mds, \
					list<DS_CNF> & l_ds) {
        if (NULL == response) {
                LOG4CXX_ERROR(g_logger, __FUNCTION__ << ", invalid args");
                return -1;
        }
        json_object * j = json_tokener_parse(response);
        if (is_error(j)) {
                LOG4CXX_ERROR(g_logger, __FUNCTION__ << ", parse response err:" << response);
                return -2;
        }
        json_object * j_result = json_object_object_get(j, "RESULT");
        json_object * j_ns_cnf = json_object_object_get(j, "NS_CNF");
        json_object * j_mds_cnfs = json_object_object_get(j, "MDS_CNFS");
        json_object * j_ds_cnfs = json_object_object_get(j, "DS_CNFS");

        if (NULL == j_result || NULL == j_ns_cnf || NULL == j_mds_cnfs || NULL == j_ds_cnfs) {
                LOG4CXX_ERROR(g_logger, __FUNCTION__ << ", invalid response:" << response);
                json_object_put(j);
                return -3;
        }
        result = json_object_get_int(j_result);
	json_object * j_ns_ip = json_object_object_get(j_ns_cnf, "IP");
	json_object * j_ns_port = json_object_object_get(j_ns_cnf, "PORT");
	json_object * j_ns_status = json_object_object_get(j_ns_cnf, "STATUS");
	json_object * j_ns_thread_num = json_object_object_get(j_ns_cnf, "THREAD_NUM");
	json_object * j_ns_socket_conn_timeout = json_object_object_get(j_ns_cnf, "SOCKET_CONN_TIMEOUT");
	json_object * j_ns_socket_idle_timeout = json_object_object_get(j_ns_cnf, "SOCKET_IDLE_TIMEOUT");
	json_object * j_ns_socket_send_timeout = json_object_object_get(j_ns_cnf, "SOCKET_SEND_TIMEOUT");
	json_object * j_ns_socket_recv_timeout = json_object_object_get(j_ns_cnf, "SOCKET_RECV_TIMEOUT");
	json_object * j_ns_socket_max_request_size = json_object_object_get(j_ns_cnf, "SOCKET_MAX_REQUEST_SIZE");
	json_object * j_ns_socket_max_response_size = json_object_object_get(j_ns_cnf, "SOCKET_MAX_RESPONSE_SIZE");
	json_object * j_ns_client_conn_pool_size = json_object_object_get(j_ns_cnf, "CLIENT_CONN_POOL_SIZE");
	json_object * j_ns_log_cnf_file = json_object_object_get(j_ns_cnf, "LOG_CNF_FILE");
	json_object * j_ns_root_data_path = json_object_object_get(j_ns_cnf, "ROOT_DATA_PATH");
	json_object * j_ns_create_time = json_object_object_get(j_ns_cnf, "CREATE_TIME");
	ns.ip = json_object_get_string(j_ns_ip);	
	ns.port = json_object_get_int(j_ns_port);	
	ns.status = json_object_get_int(j_ns_status);
	ns.thread_num = json_object_get_int(j_ns_thread_num);
	ns.socket_idle_timeout = json_object_get_int(j_ns_socket_idle_timeout);
	ns.socket_conn_timeout = json_object_get_int(j_ns_socket_conn_timeout);
	ns.socket_send_timeout = json_object_get_int(j_ns_socket_send_timeout);
	ns.socket_recv_timeout = json_object_get_int(j_ns_socket_recv_timeout);
	ns.socket_max_request_size = json_object_get_int(j_ns_socket_max_request_size);
	ns.socket_max_response_size = json_object_get_int(j_ns_socket_max_response_size);
	ns.client_conn_pool_size = json_object_get_int(j_ns_client_conn_pool_size);
	ns.log_cnf_file = json_object_get_string(j_ns_log_cnf_file);
	ns.root_data_path = json_object_get_string(j_ns_root_data_path);
	ns.create_time = json_object_get_int(j_ns_create_time);

	for(int i=0; i < json_object_array_length(j_mds_cnfs); i++) {
		json_object * j_mds_cnf = json_object_array_get_idx(j_mds_cnfs, i);
		json_object * j_ip = json_object_object_get(j_mds_cnf, "IP");
		json_object * j_port = json_object_object_get(j_mds_cnf, "PORT");
		json_object * j_status = json_object_object_get(j_mds_cnf, "STATUS");
		json_object * j_weight = json_object_object_get(j_mds_cnf, "WEIGHT");
		json_object * j_id = json_object_object_get(j_mds_cnf, "ID");
		json_object * j_thread_num = json_object_object_get(j_mds_cnf, "THREAD_NUM");
		json_object * j_socket_conn_timeout = json_object_object_get(j_mds_cnf, "SOCKET_CONN_TIMEOUT");
		json_object * j_socket_idle_timeout = json_object_object_get(j_mds_cnf, "SOCKET_IDLE_TIMEOUT");
		json_object * j_socket_send_timeout = json_object_object_get(j_mds_cnf, "SOCKET_SEND_TIMEOUT");
		json_object * j_socket_recv_timeout = json_object_object_get(j_mds_cnf, "SOCKET_RECV_TIMEOUT");
		json_object * j_socket_max_request_size = json_object_object_get(j_mds_cnf, "SOCKET_MAX_REQUEST_SIZE");
		json_object * j_socket_max_response_size = json_object_object_get(j_mds_cnf, "SOCKET_MAX_RESPONSE_SIZE");
		json_object * j_client_conn_pool_size = json_object_object_get(j_mds_cnf, "CLIENT_CONN_POOL_SIZE");
		json_object * j_log_cnf_file = json_object_object_get(j_mds_cnf, "LOG_CNF_FILE");
		json_object * j_root_data_path = json_object_object_get(j_mds_cnf, "ROOT_DATA_PATH");
		json_object * j_create_time = json_object_object_get(j_mds_cnf, "CREATE_TIME");
		MDS_CNF c;
		c.ip = json_object_get_string(j_ip);
		c.port = json_object_get_int(j_port);
		c.status = json_object_get_int(j_status);
		c.weight = json_object_get_int(j_weight);
		c.id = json_object_get_int(j_id);
		c.thread_num = json_object_get_int(j_thread_num);
		c.socket_conn_timeout = json_object_get_int(j_socket_conn_timeout);
		c.socket_idle_timeout = json_object_get_int(j_socket_idle_timeout);
		c.socket_send_timeout = json_object_get_int(j_socket_send_timeout);
		c.socket_recv_timeout = json_object_get_int(j_socket_recv_timeout);
		c.socket_max_request_size = json_object_get_int(j_socket_max_request_size);
		c.socket_max_response_size = json_object_get_int(j_socket_max_response_size);
		c.client_conn_pool_size = json_object_get_int(j_client_conn_pool_size);
		c.log_cnf_file = json_object_get_string(j_log_cnf_file);
		c.root_data_path = json_object_get_string(j_root_data_path);
		c.create_time = json_object_get_int(j_create_time);
		l_mds.push_back(c);
	}

	for(int i=0; i < json_object_array_length(j_ds_cnfs); i++) {
		json_object * j_ds_cnf = json_object_array_get_idx(j_ds_cnfs, i);
		json_object * j_ip = json_object_object_get(j_ds_cnf, "IP");
		json_object * j_port = json_object_object_get(j_ds_cnf, "PORT");
		json_object * j_status = json_object_object_get(j_ds_cnf, "STATUS");
		json_object * j_read_weight = json_object_object_get(j_ds_cnf, "READ_WEIGHT");
		json_object * j_write_weight = json_object_object_get(j_ds_cnf, "WRITE_WEIGHT");
		json_object * j_id = json_object_object_get(j_ds_cnf, "ID");
		json_object * j_mds_id = json_object_object_get(j_ds_cnf, "MDS_ID");
		json_object * j_thread_num = json_object_object_get(j_ds_cnf, "THREAD_NUM");
		json_object * j_socket_conn_timeout = json_object_object_get(j_ds_cnf, "SOCKET_CONN_TIMEOUT");
		json_object * j_socket_idle_timeout = json_object_object_get(j_ds_cnf, "SOCKET_IDLE_TIMEOUT");
		json_object * j_socket_send_timeout = json_object_object_get(j_ds_cnf, "SOCKET_SEND_TIMEOUT");
		json_object * j_socket_recv_timeout = json_object_object_get(j_ds_cnf, "SOCKET_RECV_TIMEOUT");
		json_object * j_socket_max_request_size = json_object_object_get(j_ds_cnf, "SOCKET_MAX_REQUEST_SIZE");
		json_object * j_socket_max_response_size = json_object_object_get(j_ds_cnf, "SOCKET_MAX_RESPONSE_SIZE");
		json_object * j_client_conn_pool_size = json_object_object_get(j_ds_cnf, "CLIENT_CONN_POOL_SIZE");
	
		json_object * j_log_cnf_file = json_object_object_get(j_ds_cnf, "LOG_CNF_FILE");
		json_object * j_root_data_path = json_object_object_get(j_ds_cnf, "ROOT_DATA_PATH");
		json_object * j_create_time = json_object_object_get(j_ds_cnf, "CREATE_TIME");
		DS_CNF c;
		c.ip = json_object_get_string(j_ip);
		c.port = json_object_get_int(j_port);
		c.status = json_object_get_int(j_status);
		c.read_weight = json_object_get_int(j_read_weight);
		c.write_weight = json_object_get_int(j_write_weight);
		c.id = json_object_get_int(j_id);
		c.mds_id = json_object_get_int(j_mds_id);
		c.thread_num = json_object_get_int(j_thread_num);
		c.socket_conn_timeout = json_object_get_int(j_socket_conn_timeout);
		c.socket_idle_timeout = json_object_get_int(j_socket_idle_timeout);
		c.socket_send_timeout = json_object_get_int(j_socket_send_timeout);
		c.socket_recv_timeout = json_object_get_int(j_socket_recv_timeout);
		c.socket_max_request_size = json_object_get_int(j_socket_max_request_size);
		c.socket_max_response_size = json_object_get_int(j_socket_max_response_size);
		c.client_conn_pool_size = json_object_get_int(j_client_conn_pool_size);
		c.log_cnf_file = json_object_get_string(j_log_cnf_file);
		c.root_data_path = json_object_get_string(j_root_data_path);
		c.create_time = json_object_get_int(j_create_time);
		l_ds.push_back(c);
	}

	json_object_put(j);
        return 1;
}
